package com.codefish.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codefish.entity.OutMoneyClass;
import com.codefish.entity.SysUserTokenEntity;
import com.codefish.service.OutMoneyClassService;
import com.codefish.util.Assert;
import com.codefish.util.JWTService;
import com.codefish.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 支出类型分类
 *
 * @author codefish
 * @email cool_fish@aliyun.com
 * @date 2021-07-14 15:12:43
 */
@RestController
@RequestMapping("/outMoneyClass")
public class OutMoneyClassController {
    @Autowired
    private OutMoneyClassService outMoneyClassService;

    /**
     * 新增单个
     *
     * @param token
     * @param outMoneyClass
     * @return
     */
    @PostMapping("/saveOne")
    public R saveOne(@RequestHeader("Authentication") String token
            , @RequestBody OutMoneyClass outMoneyClass) {

        Assert.isNull(outMoneyClass.getOutMoneyClassName(), "outMoneyClassName不能为空");

        SysUserTokenEntity user = JWTService.getUser(token);
        Assert.isNull(user.getId(), "当前用户无效");
        Assert.isNull(user.getFamilyId(), "当前家庭信息无效");

        // set createId / familyId
        outMoneyClass.setCreateId(user.getId());
        outMoneyClass.setFamilyId(user.getFamilyId());

        Integer id = outMoneyClassService.saveOne(outMoneyClass);

        if (id <= 0) {
            return R.error("新增失败");
        }
        return R.ok("新增成功").put("id", id);
    }

    /**
     * 修改单个
     *
     * @param token
     * @param outMoneyClass
     * @return
     */
    @PostMapping("/updateOne")
    public R updateOne(@RequestHeader("Authentication") String token
            , @RequestBody OutMoneyClass outMoneyClass) {

        Assert.isNull(outMoneyClass.getId(), "当前id不可为空");
        Assert.isNull(outMoneyClass.getOutMoneyClassName(), "outMoneyClassName不能为空");

        SysUserTokenEntity user = JWTService.getUser(token);
        Assert.isNull(user.getId(), "当前用户无效");
        Assert.isNull(user.getFamilyId(), "当前家庭信息无效");

        boolean flag = outMoneyClassService.updateOne(outMoneyClass);

        if (flag) {
            return R.ok("修改成功");
        }
        return R.error("修改失败");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteOne")
    public R deleteOne(@RequestParam("id") Integer id) {

        boolean flag = outMoneyClassService.deleteOne(id);

        if (!flag) {
            return R.error("删除失败");
        }

        return R.ok("删除成功");
    }

    /**
     * 条件分页查询
     *
     * @param token
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getPageByParams")
    public R getPageByParams(@RequestHeader("Authentication") String token
            , @RequestParam("pageNo") Integer pageNo
            , @RequestParam("pageSize") Integer pageSize) {
        Assert.isNull(pageNo, "pageNo不可为空");
        Assert.isNull(pageSize, "pageSize不可为空");

        SysUserTokenEntity user = JWTService.getUser(token);
        Assert.isNull(user.getFamilyId(), "当前用户familyId为空");

        IPage<OutMoneyClass> retPage = outMoneyClassService.getPageByParams(pageNo, pageSize, user.getFamilyId());

        return R.ok("查询成功").put("retPage", retPage);
    }

    /**
     * 根据id查询详情
     *
     * @param id
     * @return
     */
    @GetMapping("/getItemById")
    public R getItemById(@RequestParam("id") Integer id) {

        if (id <= 0) {
            return R.error("无效id，查询失败");
        }

        OutMoneyClass outMoneyClass = outMoneyClassService.getItemById(id);

        if (ObjectUtil.isNotEmpty(outMoneyClass)) {
            return R.ok("查询成功").put("data", outMoneyClass);
        }

        return R.error("查询失败");
    }
}
